package com.example.xianji.activity;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.xianji.Contant;
import com.example.xianji.XJApplication;
import com.example.xianji.R;
import com.example.xianji.constant.Constant;
import com.example.xianji.utils.sp.PreferenceEngine;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.os.Bundle;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Main_Zhanhui_DetailsActivity extends Activity {
    /********************展会详情页面************************/
    private TextView zhanhui_Title, zhanhui_details_title;
    private WebView zhanhui_details_web;
    private Button baoming_zhanhui;
    private LinearLayout linearlayout_back;
//    private SharedPreferences sp;
//    private Editor ed;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.activity_main__zhanhui__details);
        /**************初始化***********************/
        init();
        XJApplication.cachThreadPool.execute(new Runnable() {

            @Override
            public void run() {
                zhanhui_details_data();
            }
        });

        linearlayout_back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Main_Zhanhui_DetailsActivity.this.finish();
            }
        });

        baoming_zhanhui.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (PreferenceEngine.getInstance().getBoolean(Constant.SP.NAME, Constant.SP.KEY_XIANJI_DENGLU, false)) {
                    Intent intent = new Intent(Main_Zhanhui_DetailsActivity.this, Main_Submit_ZhanhuiActivity.class);
                    intent.putExtra("zhanhui_id", getIntent().getIntExtra("zhanhui_id", 0));
                    Main_Zhanhui_DetailsActivity.this.startActivity(intent);
                } else {
                    Intent intent = new Intent(Main_Zhanhui_DetailsActivity.this, Main_LoginActivity.class);
                    intent.putExtra("origin_login", 57);
                    startActivity(intent);
                }
            }
        });
    }

    private void init() {
        zhanhui_Title = (TextView) findViewById(R.id.zhanhui_Title);
        zhanhui_details_title = (TextView) findViewById(R.id.zhanhui_details_title);
        zhanhui_details_web = (WebView) findViewById(R.id.zhanhui_details_web);
        linearlayout_back = (LinearLayout) findViewById(R.id.linearlayout_back);
        baoming_zhanhui = (Button) findViewById(R.id.baoming_zhanhui);
//        sp = getSharedPreferences("text", MODE_PRIVATE);
//        ed = sp.edit();
        zhanhui_Title.setText(getIntent().getStringExtra("zhanhui_title"));
        zhanhui_details_title.setText(getIntent().getStringExtra("zhanhui_title"));
    }

    public void zhanhui_details_data() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams parms = new RequestParams();
        parms.addBodyParameter("zhanhui_id", getIntent().getIntExtra("zhanhui_id", 0) + "");
        httpUtils.send(HttpMethod.POST, Contant.zhanhui_details, parms, new RequestCallBack<String>() {

            @Override
            public void onFailure(HttpException arg0, String arg1) {
            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                try {
                    JSONObject object = new JSONObject(arg0.result);
                    if (object.getInt("status") == 1) {
                        final JSONObject OBJ = object.getJSONObject("data");
                        runOnUiThread(new Runnable() {
                            public void run() {
                                try {
                                    zhanhui_details_web.loadDataWithBaseURL(null, OBJ.getString("zcontent"), "text/html", "utf-8", null);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                    }
                } catch (JSONException e1) {
                    e1.printStackTrace();
                }
            }
        });
    }
}
